package com.wqnmdb.im.controller;

import com.wqnmdb.im.annotation.PrintData;
import com.wqnmdb.im.domain.contants.ConstantEnum;
import com.wqnmdb.im.domain.req.ReqBySessionModel;
import com.wqnmdb.im.domain.req.ReqDelSessionModel;
import com.wqnmdb.im.domain.req.ReqListMsgModel;
import com.wqnmdb.im.domain.resp.R;
import com.wqnmdb.im.exception.ParamsException;
import com.wqnmdb.im.exception.ServiceException;
import com.wqnmdb.im.service.MsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/msg")
@Api(value = "/msg", tags = {"消息服务"})
public class MsgController extends AbstractController {

    @Autowired
    private MsgService msgService;

    @PrintData
    @PostMapping("/getList")
    @ApiOperation(value = "获取会话", notes = "分页获取会话")
    public ResponseEntity<R> getList(@RequestBody @Valid ReqListMsgModel model, BindingResult bindingResult) {
        //参数验证~
        checkParams(bindingResult);
        try {
            ResponseEntity<R> result = msgService.getList(
                    model.getAccessKey(), model.getSecretKey(), model.getAppName(), model.getPageNum(), model.getPageSize());
            //返回结果~
            return result;
        } catch (Exception ex) {
            throw new ServiceException(ConstantEnum.SERVER_ERR, ex);
        }
    }

    @PrintData
    @PostMapping("/getListBySessionId")
    @ApiOperation(value = "获取消息", notes = "分页获取消息")
    public ResponseEntity<R> getListBySessionId(@RequestBody @Valid ReqBySessionModel model, BindingResult bindingResult) {
        //参数验证~
        checkParams(bindingResult);
        try {
            ResponseEntity<R> result = msgService.getListBySessionId(model.getSessionId(),
                    model.getAccessKey(), model.getSecretKey(), model.getAppName(), model.getPageNum(), model.getPageSize());
            //返回结果~
            return result;
        } catch (Exception ex) {
            throw new ServiceException(ConstantEnum.SERVER_ERR, ex);
        }
    }

    @PrintData
    @PostMapping("/delSessionById")
    @ApiOperation(value = "删除会话", notes = "删除会话")
    public ResponseEntity<R> delSessionById(@RequestBody @Valid ReqDelSessionModel model, BindingResult bindingResult) {
        //参数验证~
        checkParams(bindingResult);
        try {
            ResponseEntity<R> result = msgService.delSessionById(
                    model.getSessionId(), model.getAccessKey(), model.getSecretKey(), model.getAppName());
            //返回结果~
            return result;
        } catch (Exception ex) {
            throw new ServiceException(ConstantEnum.SERVER_ERR, ex);
        }
    }

    @PrintData
    @PostMapping("/getUserList")
    @ApiOperation(value = "获取用户", notes = "获取用户")
    public ResponseEntity<R> getUserList() {
        try {
            ResponseEntity<R> result = msgService.getUserList();
            //返回结果~
            return result;
        } catch (Exception ex) {
            throw new ServiceException(ConstantEnum.SERVER_ERR, ex);
        }
    }
}
